package com.mdtx.base.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.springframework.util.StopWatch;

/**
 * @author zhgz
 * @create 2017-08-19 15:57
 **/
public abstract class AbstractFrameMonitor {

    public static final String DAO="dao";

    protected static final String SERVICE = "service";

    protected static final String MANAGER = "manager";

    protected static final String CONTROLLER = "controller";

    protected abstract String hierarchy();

    protected void doBefore(Logger log,String method){
        log.info("--");
        log.info(this.hierarchy()+"--["+method+"] invoking");
    }

    protected void doBeforeReturn(Logger log,Object obj){
        log.info(this.hierarchy()+" return data ["+(null==obj?null:obj.toString())+"]");
    }

    protected void doAround(Logger log,String second){
        log.info(this.hierarchy()+" invoke end");
        log.info(this.hierarchy()+" time:"+second+"s");
    }

    protected void params(Logger log,String params){
        log.info(this.hierarchy()+"--params:["+params+"]");
    }

    protected String getParams(Object[] obj){

        StringBuffer pms = new StringBuffer();

        if(null == obj){
            return "";
        }
        for (int i = 0; i < obj.length; i++) {
            if(null != obj[i]){
                pms.append(obj[i].toString()+",");
            }

        }

        return pms.toString();

    }
    protected String  getTotalTime(long second){
        return second==0 ? "0.00" : second/1000+"."+second%1000;

    }

    protected  Object[] setAround(ProceedingJoinPoint joinPoint) throws Throwable {
        StopWatch watch = new StopWatch();
        Object obj=null;
        String second="0.00";
        watch.start();
        obj=joinPoint.proceed();
        watch.stop();
        second = getTotalTime(watch.getTotalTimeMillis());
        return new Object[]{obj,second};
    }


}
